package com.brillilab.service.core.instrument.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.dao.mapper.instrument.InstrumentAppointmentMapper;
import com.brillilab.domain.po.instrument.InstrumentAppointment;
import com.brillilab.service.core.instrument.IInstrumentAppointmentService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 仪器预约 服务实现类
 * </p>
 *
 * @author WuMenghao
 * @since 2019-06-12
 */
@Service
@Transactional
public class InstrumentAppointmentServiceImpl implements IInstrumentAppointmentService {

    @Resource
    private InstrumentAppointmentMapper instrumentAppointmentMapper;

    @SuppressWarnings("unchecked")
    @Override
    public List<InstrumentAppointment> selectTopThree(Long instrumentId,Integer... states) {
        Page<InstrumentAppointment> page=new Page<>(1,3);
        IPage<InstrumentAppointment> appointmentIPage=instrumentAppointmentMapper.selectPage(page,
                new LambdaQueryWrapper<InstrumentAppointment>()
                        .eq(InstrumentAppointment::getInstrumId,instrumentId)
                        .in(InstrumentAppointment::getState,states)
                        .orderByAsc(InstrumentAppointment::getStartTime));
        return appointmentIPage.getRecords();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<InstrumentAppointment>
    selectList(Long instrumentId,Integer... states) {
        return instrumentAppointmentMapper.selectList(
                new LambdaQueryWrapper<InstrumentAppointment>()
                        .eq(InstrumentAppointment::getInstrumId,instrumentId)
                        .in(InstrumentAppointment::getState,states)
                        .orderByAsc(InstrumentAppointment::getStartTime));
    }

    @Override
    public int updateById(InstrumentAppointment appointment) {
        appointment.setUpdateTime(new Date());
        return instrumentAppointmentMapper.updateById(appointment);
    }

    @Override
    public InstrumentAppointment selectById(Long id) {
        return instrumentAppointmentMapper.selectById(id);
    }

    @Override
    public int insert(InstrumentAppointment appointment) {
        return instrumentAppointmentMapper.insert(appointment);
    }

    @Override
    public IPage<InstrumentAppointment> selectPage(Integer pageNum,Integer pageSize,LambdaQueryWrapper<InstrumentAppointment> entityWrapper) {
        Page<InstrumentAppointment> page=new Page<>(pageNum,pageSize);
        return instrumentAppointmentMapper.selectPage(page,entityWrapper);
    }

    @Override
    public int selectCount(LambdaQueryWrapper<InstrumentAppointment> entityWrapper) {
        return instrumentAppointmentMapper.selectCount(entityWrapper);
    }
}
